package com.yanqu.road.server.cmd.newmonopolymkt;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.newmonopolymkt.NewMonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.newmonopolymkt.NewMonopolyMarketModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.systemgroup.newmonopolymarket.NewMonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;

@Cmd(code = GameProtocol.S_NEW_MONOPOLY_MARKET_ENTER_FROM_CROSS, desc = "进入榷场  跨服返回区服 协议")
public class MonopolyMarketEnterFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        NewMonopolyMarketProto.NewMonopolyMarketEnterRespMsgToGameServerMsg resp = NewMonopolyMarketProto.NewMonopolyMarketEnterRespMsgToGameServerMsg.parseFrom(packet.getBytes());

        NewMonopolyMarketProto.NewMonopolyMarketEnterRespMsg.Builder builder = NewMonopolyMarketProto.NewMonopolyMarketEnterRespMsg.newBuilder();
        NewMonopolyMarketModule marketModule = player.getModule(NewMonopolyMarketModule.class);
        marketModule.changeInSystemId(0);
        // 榷署弹窗
        if(marketModule.userData.getOfficeTip() != null){
            NewMonopolyMarketProto.NewMonopolyMarketOfficeTipEntity.Builder officeTip = NewMonopolyMarketProto.NewMonopolyMarketOfficeTipEntity.newBuilder();
            officeTip.setRobName(marketModule.userData.getOfficeTip().robName);
            officeTip.setLevel(marketModule.userData.getOfficeTip().level);
            officeTip.setNo(marketModule.userData.getOfficeTip().no);
            builder.setOfficeTip(officeTip);
            marketModule.userData.setOfficeTip(null);
        }
        // 资源战弹窗
        for(NewMonopolyMarketProto.NewMonopolyMarketSynPointSettlementToGameServerMsg item :  resp.getPointSettlementListList()){
            NewMonopolyMarketMgr.leaveResGrabPoint(player,item.getResGroupId(),item.getPosIndex(),false);
            // 加经验
            Property property = NewMonopolyMarketMgr.addReward(item.getType(),item.getRewardNum());
            player.getModule(CurrencyModule.class).addCurrency(property, eLogMoneyType.NewMonopolyMarket, eLogMoneyType.NewMonopolyMarketResGrabEnterSystemReward);
            // 弹窗
            NewMonopolyMarketProto.NewMonopolyMarketResGrabTipEntity.Builder tip = NewMonopolyMarketMgr.builderMonopolyMarketResGrabTipEntity(item);
            builder.addResGrabTip(tip);
        }
        marketModule.userData.setHasTip(false);
        //周日22点后，进入榷场
        //NewMonopolyMarketMgr.refreshUser(player,false,true);

        builder.setRet(ret);
        player.sendPacket(ClientProtocol.U_NEW_MONOPOLY_MARKET_ENTER, builder);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
